<?php
/*
 * DB 工厂类
 * auth: Mirze 2013-05-28
 * 
 */
class Db_Factory{
    //db config
    protected $_dsn = array();

	function __construct($db_config = '') {
        self::_config();

        $db = $this->factory();

        dump($this->_dsn);
	}

    protected function _config() {
        //$this->_dsn = ML::ini('db_dsn_pool');

    }

	/**
     * 加载数据库 支持配置文件或者 DSN
     * @access public
     * @param mixed $db_config 数据库配置信息
     * @return string
     */
    public function factory($db_config='') {
        if(empty($this->_dsn)) {
            //throw new Exception("no db config",1);
            halt('db config NULL');
        }



        // 读取数据库配置
        $db_config = $this->parseConfig($db_config);
        if(empty($db_config['dbms']))
            throw_exception(L('_NO_DB_CONFIG_'));
        // 数据库类型
        $this->dbType = ucwords(strtolower($db_config['dbms']));
        $class = 'Db'. $this->dbType;
        // 检查驱动类
        if(class_exists($class)) {
            $db = new $class($db_config);
            // 获取当前的数据库类型
            if( 'pdo' != strtolower($db_config['dbms']) )
                $db->dbType = strtoupper($this->dbType);
            else
                $db->dbType = $this->_getDsnType($db_config['dsn']);
        }else {
            // 类没有定义
            throw_exception(L('_NO_DB_DRIVER_').': ' . $class);
        }
        return $db;
    }
}